package com.huluobo.a2109mvpimdemo.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * Created by LC on 2023/11/10.
 */
public abstract class BaseFragment<V, P extends BasePresenter<V>> extends Fragment {

    protected P presenter;
    protected View view;
    private ProgressDialog progressDialog;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(bindingLayoutId(), null);
        return view;
    }

    protected abstract int bindingLayoutId();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
        if (presenter != null) {
            presenter.attachView((V) this);
        }
        loadData();
    }

    protected abstract void initData();

    protected abstract void initView();

    protected void loadData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.disAttach();
        }
    }

    public <T extends View> T findViewById(int id) {
        return view.findViewById(id);
    }

    public void showProgressDialog(String message) {
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage(message);
        progressDialog.show();
    }

    public void dismissProgressDialog() {
        if (progressDialog.isShowing() && progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
